<?php
require_once $smarty->_get_plugin_filepath('modifier', 'capitalize');

define('SEPARATOR', '-');
define('NOT_PARSED', 'N/P');

function smarty_modifier_tracklistit($string, $separator, $sequence) 
{
    if (2 == count($array = array_map('trim', explode($separator, $string, 2)))) 
    {
        switch($sequence) 
        {
            case Tracklist::SEQUENCE_ASC : {
                list($perfomer, $title) = $array;
                break;
            }
            
            case Tracklist::SEQUENCE_DESC : {
                list($title, $perfomer) = $array;
                break;
            }
        }
        
        
        static $number = 1;
        if (!preg_match('/^[0-9]+/', $perfomer)) {
            $perfomer = str_pad($number, 2, 0, STR_PAD_LEFT) . '. ' . $perfomer;
        }
        $number ++;
        
        return 
            smarty_modifier_capitalize(strtolower($perfomer), true) 
            . ' ' . constant('SEPARATOR') . ' ' . 
            smarty_modifier_capitalize(strtolower($title), true);
    }
    
    return constant('NOT_PARSED');
}